/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Diameter.java
 *
 * Created on May 22, 2009, 2:41:54 PM
 */

package snAnalysis;

import base.Utilities;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import wave.WaveInterpreter;

import java.io.IOException;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author root
 */
public class Diameter extends javax.swing.JFrame {
    MakeBlogKN blog = new MakeBlogKN();
    static WaveInterpreter win = WaveInterpreter.getInstance();



    /** Creates new form Diameter */
    public Diameter() {
        initComponents();
    }

    public String genWave(String blogID, String localIP) {
        String ws="@#"+blogID+".Ns=0.Fc=C."+
        "SQ("+
            "(RP(##.Nd==NONE.Fd+1.Nd=Fd.Nb=P)."+
            "(@#`aa'$$`192.168.1.99'.Ns=Fd)),"+
            "(@#`aa'$$`192.168.1.99'.T=Ns.Fa=1;1.Fa&Fc.Fa&Ns.T=Fa.Fa?getToDB))";
        return ws;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        MakeKN = new javax.swing.JButton();
        FindShortestPathTree = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        FindDiamter = new javax.swing.JButton();
        FindCenterNRadius = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        output = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        MakeKN.setText("Make KN");
        MakeKN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MakeKNActionPerformed(evt);
            }
        });

        FindShortestPathTree.setText("Find SPT");
        FindShortestPathTree.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FindShortestPathTreeActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Finding Diameter, Center and Radius");

        FindDiamter.setText("Diameter");
        FindDiamter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FindDiamterActionPerformed(evt);
            }
        });

        FindCenterNRadius.setText("Center & Radius");
        FindCenterNRadius.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FindCenterNRadiusActionPerformed(evt);
            }
        });

        output.setColumns(20);
        output.setRows(5);
        jScrollPane1.setViewportView(output);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Output");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(MakeKN, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                            .addComponent(FindShortestPathTree, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(FindDiamter, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(FindCenterNRadius, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 87, Short.MAX_VALUE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(119, 119, 119)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(40, 40, 40))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(MakeKN))
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                        .addGap(38, 38, 38))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(FindShortestPathTree)
                        .addGap(18, 18, 18)
                        .addComponent(FindDiamter)
                        .addGap(18, 18, 18)
                        .addComponent(FindCenterNRadius)
                        .addContainerGap())))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    public void FindEach(MakeBlogKN blog, WaveInterpreter win) throws SQLException{
        int i=0;
        win.injectWave("CR(@#`aa')");
        try{
                    Thread.sleep(5000);
                }catch(Exception e){

                }
         try {
            MySQLBridge mysql = new MySQLBridge();
            ResultSet rel = mysql.customQuery("select blogID from blog_list");
            String filter_method = Configuration.getValue("filter_method", "chordBased");
            Filter bF = new Filter(filter_method);

            
            String localIP = Configuration.getValue("localIP", "192.168.1.111");

            while (rel.next()) {
                i++;
                GenerateWaveCode gen = new GenerateWaveCode(false);

                String blogID = "`"+rel.getString("blogID")+"'";
                String localIPAd="";
                String IP = bF.getOutputIP(blogID);
                if (IP.compareTo(localIP)!=0){
                    blogID=blogID+"$$`"+IP+"'";
                    localIPAd="$$`"+localIP+"'";
                }else localIPAd="";

                String ws=genWave(blogID, localIPAd);

                System.out.println(ws);
               
                win.injectWave(ws, i);
                try{
                    Thread.sleep(3000);
                }catch(Exception e){

                }
            }
            mysql.closeConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void MakeKNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MakeKNActionPerformed

            // TODO add your handling code here:
            Utilities.debugMode = false;
            win.start();
            blog.LongWave(blog, win);
            
        
}//GEN-LAST:event_MakeKNActionPerformed

    private void FindShortestPathTreeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FindShortestPathTreeActionPerformed
        // TODO add your handling code here:
        try{
            this.FindEach(blog, win);
            System.out.println("Finished");
        }catch (Exception e){
            e.printStackTrace();
        }
}//GEN-LAST:event_FindShortestPathTreeActionPerformed

    private void FindDiamterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FindDiamterActionPerformed
        try {
            // TODO add your handling code here:
            MySQLBridge ms = new MySQLBridge();
            ResultSet se = ms.customQuery("select * from blog_list where DMax=(select max(DMax) from blog_list) limit 1");
            String DMax="";
            String blogID="";
            while (se.next()) {
                blogID=se.getString("BlogID");
                DMax=se.getString("DMax");
            }
            output.setText(DMax+" "+blogID);
            ms.closeConnection();
        } catch (SQLException ex) {
            Logger.getLogger(Diameter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_FindDiamterActionPerformed

    private void FindCenterNRadiusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FindCenterNRadiusActionPerformed
        try {

            MySQLBridge ms = new MySQLBridge();
            ResultSet se = ms.customQuery("select * from blog_list where DMax=(select min(DMax) from blog_list) limit 1");
            String DMax="";
            String blogID="";
            while (se.next()) {
                blogID=se.getString("BlogID");
                DMax=se.getString("DMax");
            }
            output.setText(DMax+" "+blogID);
            ms.closeConnection();
        } catch (SQLException ex) {
            Logger.getLogger(Diameter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_FindCenterNRadiusActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Utilities.MULTI_THREAD=true;
                new Diameter().setVisible(true);
                
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton FindCenterNRadius;
    private javax.swing.JButton FindDiamter;
    private javax.swing.JButton FindShortestPathTree;
    private javax.swing.JButton MakeKN;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea output;
    // End of variables declaration//GEN-END:variables

}
